package com.xiaoshuidi.cloud.module.finance.api.finance;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.finance.api.finance.dto.FinanceMoneyFlowRespDTO;
import com.xiaoshuidi.cloud.module.finance.convert.finance.FinanceConvert;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceMoneyFlowDO;
import com.xiaoshuidi.cloud.module.finance.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceMoneyFlowService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@RestController
public class FinanceMoneyFlowApiImpl implements FinanceMoneyFlowApi{

    @Resource
    private FinanceMoneyFlowService financeMoneyFlowService;

    /**
     * 获得流水明细信息
     * @param id
     * @param sourceBizId
     * @param sourceBizType
     * @return
     */
    @Override
    public CommonResult<FinanceMoneyFlowRespDTO> getApp(Long id, String sourceBizId, String sourceBizType) {
        FinanceMoneyFlowDO financeMoneyFlowDO;
        if (id != null) {
            financeMoneyFlowDO = financeMoneyFlowService.getById(id);
        } else if (StringUtils.hasText(sourceBizId) && StringUtils.hasText(sourceBizType)) {
            financeMoneyFlowDO = financeMoneyFlowService.getBySourceBizIdAndSourceBizType(sourceBizId, sourceBizType);
        } else {
            throw new ServiceException(ErrorCodeConstants.FINANCE_MONEY_FLOW_NOT_EXISTS);
        }
        return CommonResult.success(FinanceConvert.INSTANCE.convertRespDTO(financeMoneyFlowDO));
    }

    /**
     * 获取流水列表
     * @param sourceBizId
     * @param sourceBizType
     * @return
     */
    public CommonResult<List<FinanceMoneyFlowRespDTO>> getAppList(String sourceBizId,String sourceBizType){
        return success(FinanceConvert.INSTANCE.convertAppList(financeMoneyFlowService.getListBySourceBizIdAndSourceBizType(sourceBizId, sourceBizType)));
    }

}
